package com.kczd.jinlan.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kczd.jinlan.R;
import com.kczd.jinlan.SysApplication;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText phone;
    private EditText password;
    private TextView tv_forgetPswd;
    private ImageView mLogin;
    private ImageView mRegister;

    SweetAlertDialog pDialog = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        SysApplication.getInstance().addActivity(this);

        initViews();
    }

    private void initViews() {

        pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("Loading");
        pDialog.setCancelable(false);

        phone= (EditText) findViewById(R.id.et_phone);
        password= (EditText) findViewById(R.id.et_passwd);
        tv_forgetPswd= (TextView) findViewById(R.id.tv_forgetPswd);
        mLogin= (ImageView) findViewById(R.id.id_login);
        mRegister= (ImageView) findViewById(R.id.id_register);
        mRegister.setOnClickListener(this);
        mLogin.setOnClickListener(this);
        tv_forgetPswd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.id_login:
                if (!subpre()){
                    new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("必填验证")
                            .setContentText("你是不是忘了填什么")
                            .show();
                    return;
                }else{
                    pDialog.show();
                    /**
                     * 访问服务器，执行操作
                     */
                    if(phone.getText().toString().equals("123456789")&&password.getText().toString().equals("123456789"))
                    {
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                        SharedPreferences.Editor editor=getSharedPreferences("data",0).edit();
                        editor.putString("token","token");
                        editor.commit();
                        finish();
                    }else {
                        pDialog.dismiss();
                        new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                                .setTitleText("提示")
                                .setContentText("密码错误")
                                .show();
                    }

                }
                break;
            case R.id.id_register://注册
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                finish();
                break;
            case R.id.tv_forgetPswd://忘记密码
                startActivity(new Intent(LoginActivity.this,forgetPwdActivity.class));
                finish();
                break;
        }
    }

    /**
     * 检测用户名和密码是否为空
     * @return
     */
    public boolean subpre(){
        String loginusername= phone.getText().toString();
        String paswd= password.getText().toString();

        if(loginusername.isEmpty() || paswd.isEmpty()){
            return false;
        }
        return true;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            SysApplication.getInstance().exit();
            System.exit(0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
